package com.cictec.middleware.gps.engine.position.handler.alarms;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.cictec.middleware.commons.utils.Constants;
import com.cictec.middleware.commons.utils.DateUtils;
import com.cictec.middleware.commons.utils.PMSUtils;
import com.cictec.middleware.core.entity.WarnMsg;
import com.cictec.middleware.gps.engine.BusInstance;
import com.cictec.middleware.gps.engine.TimingPoint;
import com.cictec.middleware.gps.engine.exception.BusPositionWarnHandlerException;
import com.cictec.middleware.gps.engine.position.handler.BusPositionAlarmsHandler;
import com.cictec.middleware.gps.service.WarnMsgService;

/**
 * 停车报警
 * @author ql
 * @date 2017年9月26日
 */
@Component
public class BusStopWarnHandler implements WarnHandler {

	private static Logger logger = LoggerFactory.getLogger(BusPositionAlarmsHandler.class);
	
	@Autowired
	private WarnMsgService warnMsgService;
	
	private static int STOP_WARN_THRESHOLD = 5*60;
	
	
	@Override
	public String getWarnType() {
		return Constants.VAL_BUS_STATUS_9;
	}
	
	
	@Override
	public void onHandler(BusInstance busInstance) throws BusPositionWarnHandlerException {
	
		logger.debug("【停车报警信息处理】");
		
		String speed = busInstance.getSpeed();
//		speed = "0.00";	// 测试停车报警

		if (!speed.equals("0.00")) {
			// 有车速，清空上一个速读为0的点
			busInstance.setStartStopPoint(null);
//			logger.debug("【停车报警】{}-{} 车速大于 0, 清除报警报警开始点", busInstance.getDeviceCode(), busInstance.getBusNumber());
			
			WarnMsg warnMsg = busInstance.getWarnsByWarnType(Constants.VAL_BUS_STATUS_9);
			if (warnMsg != null) {
				logger.debug("【停车报警】{}-{} 车速大于 0, 停止停车报警，清除报警报警开始点", busInstance.getDevCode(), busInstance.getBusNumber());
				warnMsg.setWarnDuration(PMSUtils.isNull(DateUtils.secondInterval(busInstance.getTime(), DateUtils.parseDateTimes(warnMsg.getWarnStartTime()))));
				warnMsg.setWarnEndTime(DateUtils.formatDateTimes(busInstance.getTime()));
				warnMsg.setWarnEndLat(busInstance.getLatitude());
				warnMsg.setWarnEndLng(busInstance.getLongitude());
				warnMsg.setWarnUpdateTime(DateUtils.formatDateTimes(busInstance.getTime()));

				warnMsgService.saveWarn(warnMsg);
				busInstance.setWarns(Constants.VAL_BUS_STATUS_9, null);
			}
			return;
		}

		// 获取停车报警阀值
		TimingPoint stopStartPoint = busInstance.getStartStopPoint();
		if (stopStartPoint == null) {
			// 设置第一个速度为0的点
			busInstance.setStartStopPoint(busInstance);
			return;
		} else {
			// 停车报警阀值对比
			long second = DateUtils.secondInterval(stopStartPoint.getTime(), busInstance.getTime());
			
			if(second > STOP_WARN_THRESHOLD){
				WarnMsg lwarnMsg = busInstance.getWarnsByWarnType(Constants.VAL_BUS_STATUS_9);
				if (lwarnMsg != null) {
					return;
				}
				
				logger.debug("【停车报警】{}-{} 车速为 0, 停车时间大于设定阀值 {} 秒，记录停车报警", busInstance.getDevCode(), busInstance.getBusNumber(), STOP_WARN_THRESHOLD);
				// 报警开始
				WarnMsg warnMsg = new WarnMsg();
				warnMsg.setWarnBusUuid(busInstance.getBusUuid());
				warnMsg.setWarnDevUuid(busInstance.getDevUuid());

				if (!PMSUtils.isEmpty(busInstance.getDriverUuid())) {
					warnMsg.setWarnDrvUuid(busInstance.getDriverUuid());
				}
				warnMsg.setWarnLineUuid(busInstance.getLineUuid());
				warnMsg.setWarnSpeed(PMSUtils.isNull(speed));
				warnMsg.setWarnStartLat(busInstance.getLatitude());
				warnMsg.setWarnStartLng(busInstance.getLongitude());

				warnMsg.setWarnStartTime(DateUtils.formatDateTimes(busInstance.getTime()));
				warnMsg.setWarnType(Constants.VAL_BUS_STATUS_9);
				warnMsg.setWarnStorageTime(DateUtils.formatDateTimes(new Date()));

				warnMsgService.saveWarn(warnMsg);
				busInstance.setWarns(Constants.VAL_BUS_STATUS_9, warnMsg);
			}
		}

	}

	@Override
	public void stopWarn(BusInstance busInstance) {
		// TODO Auto-generated method stub
		
	}

}
